﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TimKen.DataEntity;
using TimKen.DataAccess;
using System.Data;
namespace TimKen.Web.Admin.Advertisement
{
    public partial class Addvertisement : TimKen.App.AdminPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                 AdvertisementDAO dao = new AdvertisementDAO();
                if (Request["Imtype"] != null && Request["Imtype"].ToString() != "")
                {
                    this.txt_Order.Enabled = false;
                    this.txt_Order.Text =dao.setOrderNum().ToString();
                    if (Request["Imtype"].ToString() == "2")
                    {
                        this.pp.Visible = true;
                    }
                    else {
                        this.pp.Visible = false;
                    }
                    ViewState["zt"] = Request["Imtype"].ToString();//
                    if (Request["Cz"] != null && Request["Cz"].ToString() != "")
                    {
                        ViewState["Cz"] = Request["Cz"].ToString();
                        Laguge(Request["Cz"].ToString());
                        if (Request["Cz"].ToString() == "update")
                        {
                            if(Request["id"]!=null&&Request["id"].ToString()!="")
                            {
                            
                             DataTable dt=dao.getAdvertisementByID(Request["id"].ToString()).Tables[0];
                             if (dt.Rows.Count > 0)
                             {
                                 this.txt_Pp.Text = dt.Rows[0]["pp"].ToString();
                                 this.txt_URL.Text = dt.Rows[0]["Url"].ToString();
                                 this.txt_Order.Text = dt.Rows[0]["Oby"].ToString();
                                 ViewState["path"] = dt.Rows[0]["ImagePath"].ToString();
                             }
                            
                            }
                         
                            //赋值

                        }
                    }
                    
                }
            
            }
            ScriptManager.GetCurrent(this.Page).RegisterPostBackControl(this.btn_add);
        }
        public void Laguge(string cz)
        {
            if (cz == "update")
            {
                this.btn_add.Text = GetLangString("upd");
                this.Button1.Text = GetLangString("qk");
            }
            else
            {
                this.btn_add.Text = GetLangString("Add");
                this.Button1.Text = GetLangString("qk");
            }
        }
        /// <summary>
        /// 上传
        /// </summary>
        /// <param name="fileuploadname"></param>
        /// <returns></returns>
        public string UpFiles(FileUpload fileuploadname)
        {
            string fullname = fileuploadname.FileName.ToString();//这个属性是以前2003没有的，也许是我没注意看，反正现在我才第一次用！直接取得文件名
            string url = fileuploadname.PostedFile.FileName.ToString();//这个是以前2003用的，先取得全部的上传文件路径个名字，然后再利用SubString方法来得到用户名，现在看来是没有必要了
            string typ = fileuploadname.PostedFile.ContentType.ToString();//获取文件MIME内容类型
            string typ2 = fullname.Substring(fullname.LastIndexOf(".") + 1);//获取文件名字 . 后面的字符作为文件类型
            string size = fileuploadname.PostedFile.ContentLength.ToString();
            string date = DateTime.Now.ToString("yyyyMMddHHmmss");
            string filename = "";
            //下面是保存了，我们来个判断，只能上穿传那些文件吧
            if (typ2 == "gif" || typ2 == "jpg" || typ2 == "bmp" || typ2 == "png")
            {
                filename = fullname;
               // fileuploadname.SaveAs(Server.MapPath("~/upload/image") + "/" + filename);//将文件保存在跟目录的UP文件夹下
            }
            else
            {
                Response.Write("<script>alert('文件类型不正确!');</script>");
            }
            return "upload/image/"+filename;
        }

        protected void btn_add_Click(object sender, EventArgs e)
        {
            TimKen.DataEntity.Advertisement ment = new DataEntity.Advertisement();
            AdvertisementDAO dao = new AdvertisementDAO();
            if (ViewState["Cz"] != null && ViewState["Cz"].ToString() != "")
            {
                if (ViewState["Cz"].ToString() == "update")
                {
                    string fullname = this.Ban_Img.FileName.ToString();
                     string typ2 = fullname.Substring(fullname.LastIndexOf(".") + 1);
                    if (this.Ban_Img.FileName != null && this.Ban_Img.FileName != ""&&typ2 == "gif" || typ2 == "jpg" || typ2 == "bmp" || typ2 == "png")
                    {
                        ment.ImagePath = UpFiles(this.Ban_Img);
                        System.IO.File.Delete(Server.MapPath(ViewState["path"].ToString()));
                    }
                    else {
                        ment.ImagePath ="";
                    }
                    ment.PP = this.txt_Pp != null ? this.txt_Pp.Text : "";
                    ment.Id = Request["id"] != null ? Request["id"].ToString() : "";
                    ment.Imgtype = Convert.ToInt32(ViewState["zt"] != null ? ViewState["zt"].ToString() : "99");
                    ment.Oby = Convert.ToInt32(this.txt_Order.Text);
                    ment.Url = this.txt_URL.Text;
                   bool bo= dao.updateAdvertisement(ment);
                   if (bo)
                   {
                       Response.Write("<script>alert('修改成功!');</script>");
                   }
                   else
                   {
                       Response.Write("<script>alert('修改失败!');</script>");
                   }
                }
                else
                {
                    string file = UpFiles(this.Ban_Img);
                    if (file != "upload/image/")
                    {
                        ment.PP = this.txt_Pp != null ? this.txt_Pp.Text : "";
                        ment.ImagePath = file;
                        ment.Imgtype = Convert.ToInt32(ViewState["zt"] != null ? ViewState["zt"].ToString() : "99");
                        ment.Oby = Convert.ToInt32(this.txt_Order.Text);
                        ment.Url = this.txt_URL.Text;
                        bool bo = dao.InsertImage(ment);
                        if (bo)
                        {
                            Response.Write("<script>alert('添加成功!');</script>");
                            this.txt_Order.Text = dao.setOrderNum().ToString();
                        }
                        else
                        {
                            Response.Write("<script>alert('添加失败!');</script>");
                            this.txt_Order.Text = dao.setOrderNum().ToString();
                        }
                    }
                    else {
                        //Response.Write("<script>alert('文件不能为空!');</script>");
                    }
                }
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            AdvertisementDAO dao = new AdvertisementDAO();
            this.txt_Order.Text = dao.setOrderNum().ToString();
            this.txt_URL.Text = "";
            
        } 
    }
}